// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/expression/string_writer.h>
#include <turbo/log/logging.h>
#include <melon/range.h>
#include <gtest/gtest.h>
#include <pollux/expression/vector_writers.h>
#include <pollux/testing/functions/prestosql/function_base_test.h>

namespace kumo::pollux::expressions::test {
using namespace kumo::pollux::test;

class StringWriterTest : public functions::test::FunctionBaseTest {};

TEST_F(StringWriterTest, append) {
  auto vector = make_flat_vector<StringView>(2);
  auto writer = exec::StringWriter(vector.get(), 0);
  writer.append("1 "_sv);
  writer.append(std::string_view("2 "));
  writer.append("3 "_sv);
  writer.append(std::string("4 "));
  writer.append(melon::StringPiece("5 "));

  writer.finalize();

  ASSERT_EQ(vector->value_at(0), StringView("1 2 3 4 5 "));
}

TEST_F(StringWriterTest, plusOperator) {
  auto vector = make_flat_vector<StringView>(1);
  auto writer = exec::StringWriter(vector.get(), 0);
  writer += "1 "_sv;
  writer += "2 ";
  writer += std::string_view("3 ");
  writer += std::string("4 ");
  writer += melon::StringPiece("5 ");

  writer.finalize();

  ASSERT_EQ(vector->value_at(0), "1 2 3 4 5 "_sv);
}

TEST_F(StringWriterTest, assignment) {
  auto vector = make_flat_vector<StringView>(4);

  auto writer0 = exec::StringWriter(vector.get(), 0);
  writer0 = "string0"_sv;
  writer0.finalize();

  auto writer1 = exec::StringWriter(vector.get(), 1);
  writer1 = std::string("string1");
  writer1.finalize();

  auto writer2 = exec::StringWriter(vector.get(), 2);
  writer2 = std::string_view("string2");
  writer2.finalize();

  auto writer3 = exec::StringWriter(vector.get(), 3);
  writer3 = melon::StringPiece("string3");
  writer3.finalize();

  ASSERT_EQ(vector->value_at(0), "string0"_sv);
  ASSERT_EQ(vector->value_at(1), "string1"_sv);
  ASSERT_EQ(vector->value_at(2), "string2"_sv);
  ASSERT_EQ(vector->value_at(3), "string3"_sv);
}

TEST_F(StringWriterTest, copyFromStringView) {
  auto vector = make_flat_vector<StringView>(1);
  auto writer = exec::StringWriter(vector.get(), 0);
  writer.copy_from("1 2 3 4 5 "_sv);
  writer.finalize();

  ASSERT_EQ(vector->value_at(0), "1 2 3 4 5 "_sv);
}

TEST_F(StringWriterTest, copyFromStdString) {
  auto vector = make_flat_vector<StringView>(1);
  auto writer = exec::StringWriter(vector.get(), 0);
  writer.copy_from(std::string("1 2 3 4 5 "));
  writer.finalize();

  ASSERT_EQ(vector->value_at(0), "1 2 3 4 5 "_sv);
}

TEST_F(StringWriterTest, copyFromCString) {
  auto vector = make_flat_vector<StringView>(4);
  auto writer = exec::StringWriter(vector.get(), 0);
  writer.copy_from("1 2 3 4 5 ");
  writer.finalize();

  ASSERT_EQ(vector->value_at(0), "1 2 3 4 5 "_sv);
}

TEST_F(StringWriterTest, vectorWriter) {
  auto vector = make_flat_vector<StringView>(3);
  exec::VectorWriter<Varchar> writer;
  writer.init(*vector);
  writer.setOffset(0);
  writer.current().copy_from("1 2 3");
  writer.commitNull();

  writer.setOffset(1);
  writer.current().copy_from("4 5 6");
  writer.commit(true);

  writer.setOffset(2);
  writer.current().copy_from("7 8 9");
  writer.commit(false);
  writer.finish();

  auto expected = std::vector<std::optional<std::string>>{
      std::nullopt, "4 5 6", std::nullopt};
  assertEqualVectors(vector, make_nullable_flat_vector(expected));
}
} // namespace kumo::pollux::expressions::test
